Access the raw response object via host.switchToHttp().getResponse() and call setHeader() or clearCookie() before calling res.status().json(). For Fastify use res.header() instead of setHeader(). To stay platform-agnostic use HttpAdapterHost instead of casting to a framework-specific type.
Express — res.setHeader('key', 'value') and res.clearCookie('name', options).
Fastify — res.header('key', 'value') — setHeader() does not exist on Fastify response.
To stay platform-agnostic inject HttpAdapterHost and use adapter.setHeader(res, key, value).
Always set headers before calling res.status().json() — headers cannot be set after the response starts.
Custom error headers like X-Error-Code or X-Request-Id help clients correlate errors with logs.